package com.xdl.flytest.service;

import com.xdl.flytest.bean.Report;
import com.xdl.flytest.bean.TestCase;
import com.xdl.flytest.bean.TestSuit;
import com.xdl.flytest.dao.ReportMapper;
import com.xdl.flytest.dao.TestCaseMapper;
import com.xdl.flytest.dao.TestSuitMapper;
import com.xdl.flytest.util.docker.DockerManager;
import com.xdl.flytest.util.runner.ReportUtil;
import com.xdl.flytest.util.runner.TestSuitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 80736
 * @Date: 2019/1/6 13:14
 * @Description:
 */

@Service
public class TestSuitService {
    @Autowired
    private TestSuitMapper testSuitMapper;

    @Autowired
    private TestCaseMapper testCaseMapper;


    @Autowired
    private TestSuitRunner testSuitRunner;

    @Autowired
    private ReportMapper reportMapper;

    public List<TestSuit> getAllTestSuits() {
        return testSuitMapper.selectTestSuits();
    }

    public TestSuit getTestSuitById(Integer testSuitId) {
        return testSuitMapper.selectByPrimaryKey(testSuitId);
    }

    public void addTestSuit(TestSuit testSuit) {
        testSuitMapper.insertSelective(testSuit);
    }

    public void updateTestSuit(TestSuit testSuit) {
        testSuitMapper.updateByPrimaryKeySelective(testSuit);
    }

    /**
     * 删除测试，同时删除当前测试集下的所有测试用例
     *
     * @param testSuitId
     */
    public void deleteTestSuit(Integer testSuitId) {
        testSuitMapper.deleteByPrimaryKey(testSuitId);
        testCaseMapper.deleteByTestSuitId(testSuitId);
    }

    public Integer runTestSuit(Integer id) throws IOException {

        // 获取当前测试集下所有测试用例
        List<TestCase> testCases = testCaseMapper.selectByTestSuit(id);


        try {
            // docker init
            String containerId = testSuitRunner.init();
            // 本地生成json文件
            for (TestCase testCase : testCases) {
                DockerManager.createJsonFile(testCase.getJsonData());
            }

            if (containerId != null) {
                // 运行testsuit
                testSuitRunner.run(containerId);
                // 保存测试报告到mysql
                return saveReportToDB(id);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 删除容器
            testSuitRunner.cleanUp();
        }


        return null;
    }

    /**
     * 保存测试报告到db
     *
     * @param testSuitId
     * @throws IOException
     * @return reportId 测试报告id
     */
    private Integer saveReportToDB(Integer testSuitId) throws IOException {
        Report report = new Report();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String now = sf.format(new Date());
        report.setReportName(String.format("TestSuitReport-%s", now));
        report.setReportText(ReportUtil.getReportHtml());
        report.setBelongTestSuit(testSuitId);
        report.setCreateTime(new Date());
        reportMapper.insert(report);
        return (Integer) getLastInsertPrimaryKey();

    }

    /**
     * 获取最后一次插入数据的主键
     *
     * @return
     */
    private Object getLastInsertPrimaryKey() {
        return reportMapper.selectLastInsertPrimaryKey();
    }
}
